/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package umled.model;

import java.awt.Graphics2D;
import java.util.*;
import org.w3c.dom.*;
import java.util.*;
/**
 *
 * @author ANor
 */
public class UMLClass extends UMLType {
    private List<UMLType> baseTypes = new ArrayList<UMLType>();
    private List<UMLMethod> methods = new ArrayList<UMLMethod>();
    private List<UMLData> data = new ArrayList<UMLData>();
    private boolean isAbstract = false;
    
    public UMLClass(String name, UMLElement parent, int xPos, int yPos, int width, int height, UMLVisible visible){
        super(name, parent, xPos, yPos, width, height, visible);
    }
    public void draw(Graphics2D g2d, int x, int y){
        int xDr = Math.min( x, getX() + getWidth());
        int yDr = Math.min( y, getY() + getHeight());
        drawRectangle( g2d, getX(), getY(), getX() + getWidth(), getY() + getHeight(),xDr, yDr);
        if( getWidth() > getName().length()*7 )
            drawString( g2d, getVisible().toString() + getName(), getX() + (getWidth() - getName().length() * 7 )/2, getY() + 15, xDr, yDr);
        else
            drawString( g2d, getVisible().toString() + getName(), getX() + 5, getY() + 15, xDr, yDr);
        for( UMLData d : data )
            d.draw(g2d, xDr, yDr);
        for( UMLMethod m : methods )
            m.draw(g2d, xDr, yDr);
        for( UMLType type : baseTypes )
            type.drawLink(g2d, type, xDr, yDr);
    }
    public void addBaseType(UMLType type){
        baseTypes.add(type);
    }
    public void addData(UMLData dataField){
        data.add(dataField);
    }
    public void addMethod(UMLMethod m){
        methods.add(m);
    }
    public void removeBaseType(UMLType type){
        baseTypes.remove(type);
    }
    public void removeData(UMLData d){
        data.remove(d);
    }
    public void removeMethod(UMLMethod m){
        methods.remove(m);
    }
    public List<UMLType> getBaseTypes(){
        if( baseTypes == null)
            return null;
        return Collections.unmodifiableList( baseTypes );
    }
    public List<UMLData> getData(){
        if( data == null)
            return null;
        return Collections.unmodifiableList( data );
    }
    public List<UMLMethod> getMethods(){
        if( methods == null)
            return null;
        return Collections.unmodifiableList( methods );
    }
    public void setAbstract( boolean isAbstract ){
        this.isAbstract = isAbstract;
    }
    public boolean isAbstract(){
        return isAbstract;
    }
    /**
     * Returns XML-represention of current class
     * @param document to which this class will be appended
     * @return XML-represention of current class
     */
    @Override
    public Element getXML( Document document ){
        Element element = super.getXML( document );
        element.setAttribute( "type" , "UMLClass" );
        
        //must be rewritten
        Element temp = document.createElement( "Base Types" );
        for( UMLType type : baseTypes )
            temp.appendChild( document.createTextNode( getName() ));
        element.appendChild(temp);
        
        temp = document.createElement( "Methods" );
        for( UMLMethod method : methods )
            temp.appendChild( method.getXML( document ) );
        element.appendChild(temp);
        
        temp = document.createElement( "Data" );
        for( UMLData currentData : data )
            temp.appendChild( currentData.getXML( document ) );
        element.appendChild(temp);
        
        return element;
    }
}
